Imports System.Xml

Partial Class Modules_Questionnaires_Controls_ansText
    Inherits System.Web.UI.UserControl
    Implements DotNetPm.Business.Questionnaire.Taker.IAnswerUC

    Private _config, _answerType, _questionId, _sortId As String


    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        Dim val As String() = Split(Me.Config, "||")
        _sortId = val(0)
        _questionId = val(1)
        _answerType = val(2)
        If val(3) = "0" Then
            Dim reqVal As New RequiredFieldValidator
            With reqVal
                .ID = "reqVal"
                .ControlToValidate = "txtInput"
                .Display = ValidatorDisplay.Dynamic
                .Text = "*"
                .ErrorMessage = "The answer cannot be blank!"
                '.ErrorMessage = "Question# <b>" & _questionId & "</b> : The answer of " & _answerType & " is required."
            End With
            phValidator.Controls.Add(reqVal)
        End If

        If val(6) = "0" Then
            'Expression = None
            If Not (String.IsNullOrEmpty(val(3)) And String.IsNullOrEmpty(val(4))) Then
                Dim maxminVal As New DotNetPM.Web.UI.WebControls.LengthValidator
                With maxminVal
                    .ID = "maxminVal"
                    .Display = ValidatorDisplay.Dynamic
                    .Text = "*"
                    .ControlToValidate = "txtInput"
                    .MinimumLength = val(4)
                    .MaximumLength = val(5)
                    If String.IsNullOrEmpty(val(5)) Then
                        .ErrorMessage = "Invalid answer! The minimum length of the answer is " & val(4) & "."
                        txtInput.Width = 650
                        txtInput.Rows = 12
                        txtInput.TextMode = TextBoxMode.MultiLine
                    Else
                        txtInput.MaxLength = val(5)
                        Dim width As Integer = 10 * val(5)
                        If width > 650 Then
                            txtInput.Width = 650
                            txtInput.Rows = 12
                            txtInput.TextMode = TextBoxMode.MultiLine
                        ElseIf width > 155 Then
                            txtInput.Width = width
                        End If
                        If String.IsNullOrEmpty(val(4)) Then
                            .ErrorMessage = "Invalid answer! The maximum length of the answer is " & val(5) & "."
                        Else
                            .ErrorMessage = "Invalid length of the answer! (minimum of " & val(4) & " and maximum of " & val(5) & ")"
                        End If
                    End If
                End With
                phValidator.Controls.Add(maxminVal)
            End If
        Else
            txtInput.MaxLength = val(5)
            Dim expVal As New RegularExpressionValidator
            With expVal
                .ID = "expVal"
                .ControlToValidate = "txtInput"
                .Display = ValidatorDisplay.Dynamic
                .ErrorMessage = "Invalid value (RegX)."
                If val(6) = "1" Then
                    'Expression = Custom
                    .ValidationExpression = val(7)
                Else
                    Dim myXML As XmlDocument = ExpressionXDS.GetXmlDocument
                    Dim myNode As XmlNode = myXML.SelectSingleNode("/expressions/expression[@id=" & val(6) & "]")
                    .ValidationExpression = myNode.Attributes("expression").Value
                End If
            End With
            phValidator.Controls.Add(expVal)
        End If
        txtInput.Focus()
    End Sub

    Public Property Answer() As String Implements DotNetPm.Business.Questionnaire.Taker.IAnswerUC.Answer
        Get
            Return txtInput.Text
        End Get
        Set(ByVal value As String)
            If String.IsNullOrEmpty(value) Then
                Exit Property
            End If
            txtInput.Text = value
        End Set
    End Property

    Public Property Config() As String Implements DotNetPm.Business.Questionnaire.Taker.IAnswerUC.Config
        Get
            Return _config
        End Get
        Set(ByVal value As String)
            If Not String.IsNullOrEmpty(value) Then
                _config = value
            End If
        End Set
    End Property





    'Protected Sub Page_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DataBinding
    '    Response.Write("uc_DataBinding<br/>")
    'End Sub

    'Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    '    Response.Write("uc_Init<br/>")
    'End Sub

    'Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    '    Response.Write("uc_Load<br/>")
    'End Sub


    'Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    '    Me.Page.ClientScript.RegisterStartupScript(Me.GetType, "txtAnswerCount", "<script>f2('" & txtInput.ClientID & "',600);</script>")
    'End Sub
End Class

